//
//  ReweetedStatusView.swift
//  SinaWeibo
//
//  Created by apple on 15/11/16.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import SnapKit

class ReweetedStatusView: UIView {

    
    //定义底部约束属性
    var bottomConstraints: Constraint?

    
    //定义属性赋值
    var reweetStatuses : Statues?{
        
        didSet {
            
            retweetedLabel.text = reweetStatuses?.text
            
            //首先移除约束
            self.bottomConstraints?.uninstall()
            
            if let picurls = reweetStatuses?.imageURLs where picurls.count > 0 {
                
                pictureView.hidden = false
                
                pictureView.imageURLs = picurls
               
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                    
                })

                
            } else {
                //没有配图
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                     self.bottomConstraints =  make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
                
            }
        }
    }
    // MARK 初始化
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     // MARK 设置 UI
    private func setUPUI () {
        
        //添加子视图
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        //设置约束
        
        retweetedLabel.snp_makeConstraints { (make) -> Void in
           
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            
            make.left.equalTo(retweetedLabel.snp_left)
            

        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraints =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
     // MARK 懒加载控件
    
    private lazy var retweetedLabel :UILabel = UILabel(title: "转发微博", size: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    
    private lazy var pictureView :StatusCellPictureView = StatusCellPictureView()
}
